package cn.factorybox.item.service.impl;

import cn.factorybox.common.entity.Constant;
import cn.factorybox.common.utils.DateFormatUtil;
import cn.factorybox.common.utils.UUIDUtil;
import cn.factorybox.item.mapper.CameraMapper;
import cn.factorybox.item.service.CameraService;
import cn.factorybox.model.entity.CameraImg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.text.ParseException;
import java.util.*;

/**
 * @author flypig
 * @version 1.0
 * @date 2020-05-25 9:09
 */

@Service
public class CameraServiceImpl implements CameraService {


    @Autowired
    private CameraMapper cameraMapper;

    @Override
    public boolean savePic(MultipartFile file, String deviceId) throws IOException {
        CameraImg cameraImg = new CameraImg();
        String path = Constant.IMG_PATH;
        // 判断是否有路径
        if (!new File(path).exists()) {
            new File(path).mkdirs();
        }
        // 图片后缀
        String name = file.getOriginalFilename();
        String suffixName = name.substring(name.lastIndexOf("."));
        String fileName = UUIDUtil.uuid() + suffixName;
        cameraImg.setId(UUIDUtil.uuid());
        cameraImg.setfSerialNumber(deviceId);
        cameraImg.setfImgPath(fileName);
        cameraImg.setfCreateTime(new Date());

        File tempFile = new File(path,fileName);
        if (!tempFile.getParentFile().exists()) {
            tempFile.getParentFile().mkdir();
        }
        if (tempFile.exists()) {
            tempFile.delete();
        }
        tempFile.createNewFile();
        file.transferTo(tempFile);

        int result = cameraMapper.savePic(cameraImg);
        if (result > 0) {
            return true;
        } else {
            return false;
        }

    }

    @Override
    public Map<String, Object> getTodayCount(String deviceID) {
        Map<String,Object> map = new HashMap<>();
        int count = cameraMapper.getTodayCount(deviceID);
        int target = calcauleteTarget();
        map.put("count",count);
        map.put("target",target);
        Map<String,Object> data = new HashMap<>();
        data.put("data",map);
        return data;
    }

    @Override
    public Map<String, Object> selectPicList(Map<String, Object> map) throws ParseException {
        String time = (String) map.get("endTime");
        Date date = DateFormatUtil.changeDate(time);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.HOUR,1);
        map.put("endTime",DateFormatUtil.formatDeviceTime(calendar.getTime()));
        List<CameraImg> lists = cameraMapper.selectPicList(map);
//        GregorianCalendar gregorianCalendar = new GregorianCalendar();
        Date startTime = DateFormatUtil.changeDate((String) map.get("startTime"));
        Date endTime = DateFormatUtil.changeDate((String) map.get("endTime"));
        Calendar calendar1 = Calendar.getInstance();
        calendar1.setTime(startTime);
        Calendar calendar2 = Calendar.getInstance();
        calendar2.setTime(endTime);
        int startHour = calendar1.get(calendar1.HOUR_OF_DAY);
        int endHour = calendar2.get(calendar2.HOUR_OF_DAY);
        Map<String,Object> data = changePicList(lists,startHour,endHour,calendar1);
        return data;
    }

    @Override
    public Map<String, Object> selectAllDayList(Map<String, Object> map) throws ParseException {
        List<CameraImg> lists = cameraMapper.selectAllDayPicList(map);
        List<Map<String,Object>> datas = new ArrayList<>();
        for(CameraImg cameraImg : lists) {
            Map<String,Object> data = new LinkedHashMap<>();
            data.put("id", cameraImg.getId());
            data.put("time",DateFormatUtil.formatDeviceTime(cameraImg.getfCreateTime()));
            data.put("purl", cameraImg.getfImgPath());
            data.put("targetNum",0);
            data.put("realNum",0);
            data.put("multNum",0);
            data.put("aurl","");
            datas.add(data);
        }
        Map<String,Object> data = new HashMap<>();
        data.put("data",datas);
        return data;
    }

    @Override
    public int addDevice(Map<String, Object> map) {
        String id = UUIDUtil.uuid();
        map.put("id",id);
        return cameraMapper.addDevice(map);
    }

    @Override
    public int updateDevice(Map<String, Object> map) {
        return cameraMapper.updateDevice(map);
    }

    @Override
    public Map<String, Object> selectList() {
        List<Map<String,Object>> list = cameraMapper.selectList();
        Map<String,Object> map = new HashMap<>();
        map.put("data",list);
        return map;
    }

    private static int calcauleteTarget() {
        GregorianCalendar calendar = new GregorianCalendar();
        int hour = calendar.get(Calendar.HOUR_OF_DAY);
        int minute = calendar.get(Calendar.MINUTE);
        int target = hour * 4 + minute/15;
        return target;
    }

    // 处理图像列表
    private static Map<String,Object> changePicList(List<CameraImg> lists, int startHour, int endHour, Calendar startTime) throws ParseException {
        Map<String,Object> data = new HashMap<>();
        List<Map<String,Object>> list = new ArrayList<>();
        System.out.println("结束" + endHour);
        System.out.println("开始" + startHour);
        for (int i = 0; i <= endHour-startHour; i++) {
//            System.out.println(i);
            Map<String,Object> map = new HashMap<>();
            List<Map<String,Object>> pictures = new ArrayList<>();
            map.put("time",DateFormatUtil.formatDeviceTime(startTime.getTime()));
            for(CameraImg cameraImg : lists) {
                // 循环遍历查询结果
                // 进行时间比较
                Calendar endTime = Calendar.getInstance();
                endTime.setTime(startTime.getTime());
                endTime.add(Calendar.HOUR,1);
                String time =  DateFormatUtil.formatDeviceTime(cameraImg.getfCreateTime());
                Calendar cameraTime =  Calendar.getInstance();
                cameraTime.setTime(DateFormatUtil.changeDate(time));
                if (cameraTime.after(startTime) && cameraTime.before(endTime)) {
                    Map<String,Object> picMessage = new HashMap<>();
                    picMessage.put("id", cameraImg.getId());
                    picMessage.put("time",time);
                    picMessage.put("purl", cameraImg.getfImgPath());
                    picMessage.put("targetNum",0);
                    picMessage.put("realNum",0);
                    picMessage.put("multNum",0);
                    picMessage.put("aurl","");
                    pictures.add(picMessage);
                }
            }
            map.put("picture",pictures);
            list.add(map);
            startTime.add(Calendar.HOUR,1);
        }
        data.put("data",list);
        return data;
    }
}
